<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>市场</span>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item label="商品名：">
        <el-input v-model="searchData.name" placeholder="请输入商品名"></el-input>
      </el-form-item>
      <el-form-item label="上架时间：">
        <el-date-picker
            v-model="searchData.pubdate"
            type="date"
            placeholder="请选择上架时间"
        />
      </el-form-item>
      <el-form-item label="类型：">
        <el-select placeholder="请选择" v-model="searchData.typeId">
          <el-option v-for="(type,index) in goodsTypeListData" :key="index" :label="type.name" :value="type.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否上架：">
        <el-select placeholder="请选择" v-model="searchData.flag">
          <el-option label="上架" value="1"></el-option>
          <el-option label="下架" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getGoodsList">搜索</el-button>
        <el-button @click="resetParams">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 用户列表 -->
    <el-table :data="goodsListData" style="width: 100%">
      <el-table-column label="商品名" prop="name"></el-table-column>
      <el-table-column label="价格" prop="price">
      </el-table-column>
      <el-table-column label="上架日期" prop="pubdateStr"></el-table-column>
      <el-table-column label="类型" prop="goodsTypeName"></el-table-column>
      <el-table-column label="简介" prop="intro"></el-table-column>
      <el-table-column label="图片" prop="picture">
        <template #default="{row}">
          <el-image
              :src="row.picture ? `/api/file/download.do?filename=${encodeURIComponent(row.picture)}` : '/default-book.png'"
              :preview-src-list="[row.picture ? `/api/file/download.do?filename=${encodeURIComponent(row.picture)}` : '/default-book.png']"
              hide-on-click-modal
              fit="cover"
          >
  <template #error>
    <div class="image-error-slot">
      <img src="/default-book.png" alt="默认图书封面" style="width: 100%; height: 100%; object-fit: cover;">
    </div>
  </template>
</el-image>
        </template>
      </el-table-column>

      <el-table-column label="添加时间" prop="createTimeStr"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-tooltip content="收藏" placement="top">
            <el-button :icon="Star" circle plain type="warning" @click="collectGoods(row)"></el-button>
          </el-tooltip>
          <el-tooltip content="查看详情" placement="top">
            <el-button :icon="Edit" circle plain type="primary" @click="viewDetail(row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"/>
      </template>
    </el-table>
    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[5 ,10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange" style="margin-top: 20px; justify-content: flex-end"/>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import { Edit, Star } from "@element-plus/icons-vue";
import {goodsList, goodsTypeList} from "@/api/goods";
import {ElMessage} from "element-plus";

// 条件查询数据模型
const searchData = ref({
  name: '',
  pubdate: '',
  typeId: '',
  flag: ''
})
//分页条数据模型
const pageNo = ref(1)//当前页
const total = ref(0)//总条数
const pageSize = ref(5)//每页条数
// 商品列表数据集合
const goodsListData = ref([])
// 商品类型列表数据集合
const goodsTypeListData = ref([])



// 条件分页查询商品列表
const getGoodsList = () => {
  const params = {
    params: {
      pageNo: pageNo.value,
      pageSize: pageSize.value,
      ...searchData.value
    }
  }
  goodsList(params).then(res=>{
    if (res.success) {
      goodsListData.value= res.data.records
      total.value = res.data.count
    }
  })
}

// 查询商品类型
const getGoodsTypeList = () => {
  goodsTypeList().then(res=>{
    if (res.success) {
      goodsTypeListData.value= res.data
    }
  })
}

onMounted(()=>{
  getGoodsTypeList()
  getGoodsList()
})

// 重置条件查询表单
const resetParams = () => {
  searchData.value={
    name: '',
    pubdate: '',
    typeId: '',
    flag: ''
  }
  getGoodsList()
}

const collectGoods = async (row) => {

};
// 查看商品详情
const viewDetail = (row) => {
  ElMessage.info(`查看商品详情：${row.name}`)
  // 这里可以添加查看详情的逻辑，如弹出详情对话框等
}

// 当每页条数发生了变化，会调用
const handleSizeChange = (val) => {
  pageSize.value = val
  getGoodsList()
}

// 当当前页面发生变化，会调用
const handleCurrentChange = (val) => {
  pageNo.value = val
  getGoodsList()
}
</script>

<style scoped lang="scss">
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>